export default {
  namespaced:true,
  
  state:()=>({
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    token: uni.getStorageSync('token') || '', 
    userinfo: JSON.parse(uni.getStorageSync('userinfo')|| '{}'),
      // 重定向的 object 对象 { openType, from }
    redirectInfo: null
  }),
  mutations:{
    //更新收货地址
    updateAddress(state,address){
      state.address = address
      
      this.commit('m_user/saveAddressToStore')
    },
    saveAddressToStore(state){
      uni.setStorageSync('address',JSON.stringify(state.address))
    },
    // 更新用户信息
    updateUserInfo(state,userInfo){
      state.userInfo = userInfo
      this.commit('m_user/saveUserInfo2Storage')
    },
    //保存用户信息到storage
    saveUserInfo2Storage(state){
      uni.setStorageSync('userinfo',JSON.stringify(state.userInfo))
    },
    updateToken(state,token){
      state.token = token
      this.commit('m_user/saveToStorageToken')
    },
    saveToStorageToken(state){
      uni.setStorageSync('token',state.token)
    },
    updateRedirectInfo(state,info){
      state.redirectInfo = info
      console.log(state.redirectInfo)
    }
  },
  getters:{
    addstr(state){
        // 收货详细地址的计算属性
      if(!state.address.provinceName) return ''
          // 拼接 省，市，区，详细地址 的字符串并返回给用户
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
}